package com.cloud.tsp.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.core.exception.MyException;
import com.cloud.api.domain.forward.Platform;
import com.cloud.api.domain.forward.QueryPlatform;
import com.cloud.api.domain.forward.UpdateStatus;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.PlatformMapper;
import com.cloud.tsp.service.IPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2024-01-08
 */
@Service("platformService")
public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform> implements IPlatformService {

    @Resource
    private PlatformMapper platformMapper;
    @Autowired
    private StreamBridge streamBridge;

    @Override
    public TableDataInfo<Platform> selectPlatformList(QueryPlatform para) {
        Page<Platform> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<Platform> data = platformMapper.selectPlatformList(page, para);
        return TableDataInfo.build(data);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void InsertOrUpdate(Platform para) {
        if (StringUtil.isNotBlank(para.getId())){
            Platform platform = platformMapper.selectById(para.getId());
            if (platform != null && platform.getConnectionStatus() != 0){
                throw new MyException("wds.exception.platConnect");
            }
        }
        para.setConnectionStatus((byte) 0);
        this.saveOrUpdate(para);
        para.setType("edit");
        mqNotice(para);
    }

    @Override
    public void deletedById(String id) {
        Platform platform = platformMapper.selectById(id);
        if (platform != null){
            if (platform.getConnectionStatus() != 0){
                throw new MyException("wds.exception.platConnect");
            }
            platformMapper.deleteById(id);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateStatus(UpdateStatus para) {
        Platform query = platformMapper.selectById(para.getId());
        if (Objects.equals(para.getStatus(), query.getConnectionStatus())){
            String msg = "";
            if (para.getStatus()==0){
                msg = "wds.exception.connectOff";
            }else {
                msg = "wds.exception.connectOn";
            }
            throw new MyException(msg);
        }
        Platform platform = new Platform();
        platform.setId(para.getId());
        platform.setConnectionStatus(para.getStatus());
        platformMapper.updateById(platform);
        if (para.getStatus()==0){
            query.setType("delete");
        } else if (para.getStatus() == 1) {
            query.setType("edit");
        }
        mqNotice(query);
    }

    public void mqNotice(Platform para){
        boolean send = streamBridge.send("tspplatform-out-0", MessageBuilder.withPayload(para).build());
        System.out.println(send);
    }
}